package com.qc.wintrax.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;
import com.qc.wintrax.WTApplication;
import com.qc.wintrax.utils.CombineImage;
import com.qc.wintrax.utils.LogUtil;
import com.qc.wintrax.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MoreMarkerActivity extends BaseActivity {

    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.save_tv)
    TextView saveTv;

    @Bind(R.id.scenery_cb)
    CheckBox sceneryCb;
    @Bind(R.id.scenery_et)
    EditText sceneryEt;
    @Bind(R.id.custom_cb)
    CheckBox customCb;
    @Bind(R.id.custom_et)
    EditText customEt;
    @Bind(R.id.start_cb)
    CheckBox startCb;
    @Bind(R.id.sp_cb)
    CheckBox spCb;
    @Bind(R.id.cp_cb)
    CheckBox cpCb;
    @Bind(R.id.finish_cb)
    CheckBox finishCb;
    @Bind(R.id.medical_cb)
    CheckBox medicalCb;
    @Bind(R.id.wc_cb)
    CheckBox wcCb;
    @Bind(R.id.photography_cb)
    CheckBox photographyCb;
    @Bind(R.id.start_img)
    ImageView startImg;
    @Bind(R.id.sp_img)
    ImageView spImg;
    @Bind(R.id.scenery_txt)
    TextView sceneryTxt;
    @Bind(R.id.scenery_img)
    ImageView sceneryImg;
    @Bind(R.id.custom_txt)
    TextView customTxt;
    @Bind(R.id.custom_img)
    ImageView customImg;
    @Bind(R.id.cp_img)
    ImageView cpImg;
    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.finish_img)
    ImageView finishImg;
    @Bind(R.id.medical_img)
    ImageView medicalImg;
    @Bind(R.id.wc_img)
    ImageView wcImg;
    @Bind(R.id.photography_img)
    ImageView photographyImg;

    private LatLng latLng;
    private int checkTag = 0;
    private String biaoji;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_marker);
        ButterKnife.bind(this);
        latLng = getIntent().getParcelableExtra("latLng");
    }

    public void Judgement() {
        if (checkTag == 1) {
            sceneryCb.setChecked(true);
            customCb.setChecked(false);
            startCb.setChecked(false);
            finishCb.setChecked(false);
            spCb.setChecked(false);
            cpCb.setChecked(false);
            wcCb.setChecked(false);
            photographyCb.setChecked(false);
            medicalCb.setChecked(false);
        } else if (checkTag == 2) {
            customCb.setChecked(true);
            sceneryCb.setChecked(false);
            startCb.setChecked(false);
            finishCb.setChecked(false);
            spCb.setChecked(false);
            cpCb.setChecked(false);
            wcCb.setChecked(false);
            photographyCb.setChecked(false);
            medicalCb.setChecked(false);
        } else if (checkTag == 3) {
            customCb.setChecked(false);
            sceneryCb.setChecked(false);
        }
    }

    public Bitmap setBitmap() {
        Bitmap bmp = null;
        if (checkTag == 1 || checkTag == 2) {
            if (checkTag == 1) {
                bmp = ((BitmapDrawable) sceneryImg.getDrawable()).getBitmap();
            } else if (checkTag == 2) {
                bmp = ((BitmapDrawable) customImg.getDrawable()).getBitmap();
            }
        }
        return bmp;
    }

    public List<Bitmap> setBitmapList() {
        List<Bitmap> bitmapList = new ArrayList<Bitmap>();
        Bitmap startbmp = ((BitmapDrawable) startImg.getDrawable()).getBitmap();
        Bitmap spbmp = ((BitmapDrawable) spImg.getDrawable()).getBitmap();
        Bitmap cpbmp = ((BitmapDrawable) cpImg.getDrawable()).getBitmap();
        Bitmap finbmp = ((BitmapDrawable) finishImg.getDrawable()).getBitmap();
        Bitmap medbmp = ((BitmapDrawable) medicalImg.getDrawable()).getBitmap();
        Bitmap wcbmp = ((BitmapDrawable) wcImg.getDrawable()).getBitmap();
        Bitmap phobmp = ((BitmapDrawable) photographyImg.getDrawable()).getBitmap();
        if (startCb.isChecked()) {
            bitmapList.add(startbmp);
        }
        if (spCb.isChecked()) {
            bitmapList.add(spbmp);
        }
        if (cpCb.isChecked()) {
            bitmapList.add(cpbmp);
        }
        if (finishCb.isChecked()) {
            bitmapList.add(finbmp);
        }
        if (medicalCb.isChecked()) {
            bitmapList.add(medbmp);
        }
        if (wcCb.isChecked()) {
            bitmapList.add(wcbmp);
        }
        if (photographyCb.isChecked()) {
            bitmapList.add(phobmp);
        }
        return bitmapList;
    }

    @OnClick({R.id.back_iv, R.id.save_tv, R.id.scenery_cb, R.id.custom_cb, R.id.start_cb, R.id.sp_cb, R.id.cp_cb, R.id.finish_cb, R.id.medical_cb, R.id.wc_cb})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                WTApplication.getInstance().finishActivity();
                break;
            case R.id.save_tv:
                if (checkTag == 0) {
                    WTApplication.getInstance().finishActivity();
                } else if (checkTag == 1) {
                    Intent intent = new Intent(context, CamerActivity.class);
                    startActivity(intent);
                    overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
                } else if (checkTag == 2) {
                    Intent intent = new Intent();
                    intent.putExtra("biaoji", biaoji+"11111");
                    Bitmap bms = setBitmap();
                    intent.putExtra("Bitmap", bms);
                    setResult(RESULT_OK, intent);
                    WTApplication.getInstance().finishActivity();
                } else if (checkTag == 3) {
                    Intent intent = new Intent();
                    intent.putExtra("biaoji", biaoji);
//                    Bitmap bm = CombineImage.combineImageView(startImg, spImg);
//                   d Bitmap bms = setBitmap();
                    List<Bitmap> newBitmapList = setBitmapList();
                    if(newBitmapList.size() <5) {
                        Bitmap bmsmap = null;
                        if (newBitmapList.size() == 1) {
                            bmsmap = newBitmapList.get(0);
                        } else if (newBitmapList.size() == 2) {
                            bmsmap = CombineImage.combineImageView(newBitmapList.get(0), newBitmapList.get(1));
                        } else if (newBitmapList.size() == 3) {
                            Bitmap newBp = CombineImage.combineImageView(newBitmapList.get(0), newBitmapList.get(1));
                            bmsmap = CombineImage.combineImageView(newBp, newBitmapList.get(2));
                        } else if (newBitmapList.size() == 4) {
                            Bitmap staBp = CombineImage.combineImageView(newBitmapList.get(0), newBitmapList.get(1));
                            Bitmap twoBp = CombineImage.combineImageView(newBitmapList.get(2), newBitmapList.get(3));
                            bmsmap = CombineImage.combineImageView(staBp, twoBp);
                        }
                        LogUtil.e(newBitmapList.size() + "");
                        intent.putExtra("Bitmap", bmsmap);
                        setResult(RESULT_OK, intent);
                        WTApplication.getInstance().finishActivity();
                    }else {
                        ToastUtil.show(context,getResources().getString(R.string.markers_hint));
                    }
                }
                break;
            case R.id.scenery_cb:
                checkTag = 1;
                biaoji = "实景";
                Judgement();
                break;
            case R.id.custom_cb:
                checkTag = 2;
                biaoji = "自定义";
                Judgement();
                break;
            case R.id.start_cb:
                checkTag = 3;
                Judgement();
                break;
            case R.id.sp_cb:
                checkTag = 3;
                Judgement();
                break;
            case R.id.cp_cb:
                checkTag = 3;
                Judgement();
                break;
            case R.id.finish_cb:
                checkTag = 3;
                Judgement();
                break;
            case R.id.medical_cb:
                checkTag = 3;
                Judgement();
                break;
            case R.id.wc_cb:
                checkTag = 3;
                Judgement();
                break;
        }
    }
}
